package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.ContractDto;
import com.qf.commerce.common.dto.ContractSelectDto;
import com.qf.commerce.entity.Contract;
import com.qf.commerce.exception.ControllerException;
import com.qf.commerce.exception.DaoException;
import com.qf.commerce.exception.ServiceException;
import com.qf.commerce.mapper.ContractMapper;
import com.qf.commerce.resp.ResponseStatus;
import com.qf.commerce.service.ContractService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/*
 * @author 358
 * @since JDK 1.8
 * @data 2023/9/15 17:16
*/

@Service
public class ContractServiceImpl implements ContractService{

    @Resource
    private ContractMapper contractMapper;
    @Override
    public Contract selectByPrimaryKey(Integer contractId) {
        return contractMapper.selectByPrimaryKey(contractId);
    }

    @Override
    public int updateByPrimaryKeySelective(Contract record) {
        return contractMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Contract record) {
        return contractMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<Contract> list(Integer page, Integer size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> contractMapper.selectAll());
    }

    @Override
    public int add(ContractDto contractDto) {
        Contract contract = new Contract();
        if (ObjectUtils.isEmpty(contract) || ObjectUtils.isEmpty(contractDto)) {
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        BeanUtils.copyProperties(contractDto, contract);
        contract.setStatus(1);
        return contractMapper.insert(contract);
    }

    @Override
    public int updateStatus(Integer contractId) {
        if(contractId==null){
            throw new ControllerException(ResponseStatus.DATA_ERROR);
        }
        int num=contractMapper.updateStatusById(contractId,0);
        if(num>0){
            throw new DaoException(ResponseStatus.SYS_ERROR);
        }
        return num;
    }

    @Override
    public PageInfo<Contract> select(ContractSelectDto selectDto) {
        return PageHelper.startPage(selectDto.getPage(), selectDto.getSize()).doSelectPageInfo(() -> contractMapper.select(selectDto));
    }
}
